package org.example.sbdemo.service;

import org.example.sbdemo.DTO.UserDTO;
import org.example.sbdemo.converter.UserConverter;
import org.example.sbdemo.dao.UserDao;
import org.example.sbdemo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserSImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public UserDTO findById(Integer id) {
        User user = userDao.findById(id).orElseThrow(() -> new IllegalArgumentException("Not valid id: " + id));
        return UserConverter.UserConvert(user);

    }

    @Override
    public List<UserDTO> findAll() {
        List<User> users = userDao.findAll();
        List<UserDTO> userDTOs = new ArrayList<>();
        for (User user : users) {
            userDTOs.add(UserConverter.UserConvert(user));
        }
        return userDTOs;
    }

    @Override
    public Integer addUser(UserDTO userDTO) {
        User user = UserConverter.UserConvert(userDTO);

        if (user.getId() == null) {
            // 优先用 DAO 的原生查询获取最小缺失 id
            Integer missing = userDao.findSmallestMissingId();
            if (missing == null || missing <= 0) {
                Integer max = userDao.findMaxId();
                missing = (max == null ? 0 : max) + 1;
            }
            user.setId(missing);
        }

        try {
            User saved = userDao.save(user);
            return saved.getId();
        } catch (org.springframework.dao.DataIntegrityViolationException ex) {
            // 如果发生主键冲突，回退并尝试使用 max+1
            Integer max = userDao.findMaxId();
            int candidate = (max == null ? 0 : max) + 1;
            user.setId(candidate);
            User saved = userDao.save(user);
            return saved.getId();
        }
    }

    @Override
    public void deleteById(Integer id) {
        userDao.findById(id).orElseThrow(()->new IllegalArgumentException("id "+ id + " not found."));
        userDao.deleteById(id);
    }

    @Override
    public UserDTO update(UserDTO userDTO) {
        // 确保用户存在
        userDao.findById(userDTO.getId()).orElseThrow(() -> new IllegalArgumentException("Not valid id: " + userDTO.getId()));
        User user = userDao.save(UserConverter.UserConvert(userDTO));

        return UserConverter.UserConvert(user);
    }

    @Override
    public List<UserDTO> findAllByGenderM() {
        List<User> users = userDao.findByGender("male");
        List<UserDTO> userDTOs = new ArrayList<>();
        for (User user : users) {
            userDTOs.add(UserConverter.UserConvert(user));
        }
        return userDTOs;
    }

    @Override
    public List<UserDTO> findAllByGenderF() {
        List<User> users = userDao.findByGender("female");
        List<UserDTO> userDTOs = new ArrayList<>();
        for (User user : users) {
            userDTOs.add(UserConverter.UserConvert(user));
        }
        return userDTOs;
    }
}
